﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BookStoreLib.Dal
{
    /// <summary>
    /// 
    /// </summary>
    class CategoryDal : EntityDal<Category>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nID"></param>
        /// <returns></returns>
        protected internal override Category GetSingle(int nID)
        {
            try
            {
                return bookDB.Categories.SingleOrDefault(category => category.ID == nID);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected internal override List<Category> GetList()
        {
            try
            {
                return bookDB.Categories.ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strName"></param>
        /// <returns></returns>
        protected internal override List<Category> GetList(String strName)
        {
            try
            {
                return bookDB.Categories.Where(category => category.Name.ToLower()
                    .Contains(strName.ToLower())).ToList();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        protected internal override Boolean Add(Category category)
        {
            try
            {
                bookDB.Categories.Add(category);
                bookDB.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        protected internal override Boolean Update(Category category)
        {
            return true;
        }
    }
}
